layui.use(['table', 'admin', 'ax', 'func','laydate','formSelects', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;

    var enumAjax = new $ax(Feng.ctxPath + "/enums/productTypes");
    var enumRes = enumAjax.start();
    var projects = enumRes.data;
    var html = "";
    for(var key in projects){
        html += '<option value="'+key+'">'+projects[key]+'</option>'
    }
    $("#type").append(html);
    form.render('select');

    /**
     * 运营组-商品管理
     */
    var NanSellerProducts = {
        tableId: "nanSellerProductsTable"
    };
    var enumAjax = new $ax(Feng.ctxPath + "/enums/checkStatus");
    var enumRes = enumAjax.start();
    var projects = enumRes.data;
    var html = "";
    for(var key in projects){
        html += '<option value="'+key+'">'+projects[key]+'</option>'
    }
    $("#checkStatus").append(html);
    form.render('select');

    /**
     * 初始化表格的列
     */
    NanSellerProducts.initColumn = function () {
        return [[
            {align: 'center', toolbar: '#tableBar', title: '操作', width: 160},
            {field: 'id', sort: true, title: '商品ID'},
            {field: 'title', sort: false, title: '商品名'},
            {field: 'typeStr', sort: false, title: '商品类型', width: 160},
            {field: 'isOnline', sort: false, title: '状态', templet: '<div>{{d.isOnline? "在售": "已下架"}}</div>'},
            {field: 'thumb', sort: false, title: '主图', templet: '<div><img src="{{Feng.cdn}}{{d.thumb}}" alt="" style="max-width:80px;max-height:60px;" /></div>'},
            {field: 'adImg', sort: false, title: '广告图', templet: function(d){
                // 只有新品推荐(type=0)且有广告图时才显示
                if(d.type === 0 && d.adImg){
                    return '<div><img src="' + Feng.cdn + d.adImg + '" alt="" style="max-width:80px;max-height:60px;" /></div>';
                } else {
                    return '<div>-</div>';
                }
            }},
            {field: 'link', sort: false, title: '外站链接'},
            {field: 'createTime', sort: false, title: '创建时间'},
            {field: 'updateTime', sort: false, title: '更新时间'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    NanSellerProducts.search = function () {
        var queryData = {};

        queryData['title'] = $('#title').val();
        queryData['type'] = $('#type').val();
        queryData['isOnline'] = $('#isOnline').val();
        table.reload(NanSellerProducts.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 跳转到添加页面
     */
    NanSellerProducts.jumpAddPage = function () {
        window.location.href = Feng.ctxPath + '/nanSellerProducts/add'
    };

    window.reloadPage = function(){
        table.reload(NanSellerProducts.tableId);
    }
    /**
     * 跳转到编辑页面
     *
     * @param data 点击按钮时候的行数据
     */
    NanSellerProducts.jumpEditPage = function (data) {
        window.location.href = Feng.ctxPath + '/nanSellerProducts/edit?id=' + data.id
    };

    /**
     * 导出excel按钮
     */
    NanSellerProducts.exportExcel = function () {
        var checkRows = table.checkStatus(NanSellerProducts.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    NanSellerProducts.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/nanSellerProducts/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(NanSellerProducts.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    NanSellerProducts.onEditPage = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/nanSellerProducts/editItem", function (data) {
                Feng.success("操作成功!");
                table.reload(NanSellerProducts.tableId);
            }, function (data) {
                Feng.error("操作失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否通过?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + NanSellerProducts.tableId,
        url: Feng.ctxPath + '/nanSellerProducts/list',
        page: true,
        limit: 20,
        height: "full-158",
        cellMinWidth: 100,
        cols: NanSellerProducts.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        NanSellerProducts.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

        NanSellerProducts.jumpAddPage();

    });

    // 导出excel
    $('#btnExp').click(function () {
        NanSellerProducts.exportExcel();
    });

    table.on('sort('+NanSellerProducts.tableId+')', function(obj){
        table.reload(NanSellerProducts.tableId, {
            initSort: obj,
            where: {
                field: obj.field.replace(/([A-Z])/g,"_$1").toLowerCase()
                ,order: obj.type
            }
        });
    });

    // 工具条点击事件
    table.on('tool(' + NanSellerProducts.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            NanSellerProducts.jumpEditPage(data);
        }else if(layEvent === 'delete'){
            NanSellerProducts.onDeleteItem(data);
        }
    });
});
